package com.wust.data;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wust.database.DatabaseFactory;
import com.wust.util.ConstDefine;

public class MapDao {
	private Context context;
	private List<KnowledgeMap>maplist;
	public MapDao(Context context)
	{
		this.context=context;
	}
	public List<KnowledgeMap> queryMap()
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.EXIST_DATABASE);
		SQLiteDatabase db=database.getReadableDatabase();
		maplist=new ArrayList<KnowledgeMap>();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.MAP_TABLE, null, null, null, null, null, null);
		while(cursor.moveToNext())
		{
			KnowledgeMap map=new KnowledgeMap();
			map.setId(cursor.getInt(cursor.getColumnIndex("id")));
			map.setCat_name(cursor.getString(cursor.getColumnIndex("cat_name")));
			map.setCommend(cursor.getString(cursor.getColumnIndex("commend")));
			map.setDel_flag(cursor.getString(cursor.getColumnIndex("del_flag")));
			map.setGrandpa_id(cursor.getInt(cursor.getColumnIndex("grandpa_id")));
			map.setParent_id(cursor.getInt(cursor.getColumnIndex("parent_id")));
			map.setRank(cursor.getString(cursor.getColumnIndex("rank")));
			maplist.add(map);
		}
		db.close();
		return maplist;
	}
	public List<KnowledgeMap> queryMapByparentId(String parentId)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.EXIST_DATABASE);
		SQLiteDatabase db=database.getReadableDatabase();
		maplist=new ArrayList<KnowledgeMap>();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.MAP_TABLE, null, "parent_id=?", new String[]{parentId}, null, null, null);
		while(cursor.moveToNext())
		{
			KnowledgeMap map=new KnowledgeMap();
			map.setId(cursor.getInt(cursor.getColumnIndex("id")));
			map.setCat_name(cursor.getString(cursor.getColumnIndex("cat_name")));
			map.setCommend(cursor.getString(cursor.getColumnIndex("commend")));
			map.setDel_flag(cursor.getString(cursor.getColumnIndex("del_flag")));
			map.setGrandpa_id(cursor.getInt(cursor.getColumnIndex("grandpa_id")));
			map.setParent_id(cursor.getInt(cursor.getColumnIndex("parent_id")));
			map.setRank(cursor.getString(cursor.getColumnIndex("rank")));
			maplist.add(map);
		}
		db.close();
		return maplist;
	}
	public KnowledgeMap getMapById(String id)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.EXIST_DATABASE);
		SQLiteDatabase db=database.getReadableDatabase();
		KnowledgeMap map=new KnowledgeMap();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.MAP_TABLE, null, "id=?", new String[]{id}, null, null, null);
		while(cursor.moveToNext())
		{
		
			map.setId(cursor.getInt(cursor.getColumnIndex("id")));
			map.setCat_name(cursor.getString(cursor.getColumnIndex("cat_name")));
			map.setCommend(cursor.getString(cursor.getColumnIndex("commend")));
			map.setDel_flag(cursor.getString(cursor.getColumnIndex("del_flag")));
			map.setGrandpa_id(cursor.getInt(cursor.getColumnIndex("grandpa_id")));
			map.setParent_id(cursor.getInt(cursor.getColumnIndex("parent_id")));
			map.setRank(cursor.getString(cursor.getColumnIndex("rank")));
			
		}
		db.close();
		return map;
	}
}
